package com.ut.demo.slice;

import com.ut.animator.AnimationListener;
import com.ut.animator.RichPathAnimator;
import com.ut.demo.ResourceTable;
import com.ut.richpath.RichPath;
import com.ut.richpath.RichPathView;
import com.ut.richpath.util.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

    RichPathView androidRichPathView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        _init();
    }

    private void _init(){
        androidRichPathView = (RichPathView) findComponentById(ResourceTable.Id_ic_android);
        androidRichPathView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                animateAndroid();
            }
        });

        findComponentById(ResourceTable.Id_animationSamplesButton).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                openAnimationSamples(component);
            }
        });

        findComponentById(ResourceTable.Id_compoundViewSamplesButton).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                openCompoundViewSamples(component);
            }
        });
    }

    public void animateAndroid(Component component) {
        animateAndroid();
    }

    private void animateAndroid() {

        final List<RichPath> allPaths = androidRichPathView.findAllRichPaths();
        final RichPath head = androidRichPathView.findRichPathByName("head");
        final RichPath body = androidRichPathView.findRichPathByName("body");
        final RichPath rHand = androidRichPathView.findRichPathByName("r_hand");
        final RichPath lHand = androidRichPathView.findRichPathByName("l_hand");
//        RichPathAnimator.animate(rHand).rotation(0,90).duration(1500).start();
/*

        AnimatorValue animator_a = new AnimatorValue();
        animator_a.setCurveType(Animator.CurveType.LINEAR);
        animator_a.setDuration(1000);
        animator_a.setLoopedCount(2);
        animator_a.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                LogUtil.i("animator value: %f", v);
            }
        });
        AnimatorValue animator_b = new AnimatorValue();
        animator_b.setCurveType(Animator.CurveType.LINEAR);
        animator_b.setDuration(1000);
        animator_b.setLoopedCount(2);
        animator_b.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                LogUtil.i("animator value: %f", v);
            }
        });
        AnimatorValue animator_c = new AnimatorValue();
        animator_c.setCurveType(Animator.CurveType.LINEAR);
        animator_c.setDuration(1000);
        animator_c.setLoopedCount(2);
        animator_c.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                LogUtil.i("animator value: %f", v);
            }
        });
        AnimatorValue animator_d = new AnimatorValue();
        animator_d.setCurveType(Animator.CurveType.LINEAR);
        animator_d.setDuration(1000);
        animator_d.setLoopedCount(2);
        animator_d.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                LogUtil.i("animator value: %f", v);
            }
        });
        AnimatorValue animator_e = new AnimatorValue();
        animator_e.setCurveType(Animator.CurveType.LINEAR);
        animator_e.setDuration(1000);
        animator_e.setLoopedCount(2);
        animator_e.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                LogUtil.i("animator value: %f", v);
            }
        });
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.runParallel(animator_a, animator_b, animator_c, animator_d, animator_e);
        animatorGroup.start();

*/


//        LogUtil.i("allPaths: %s", new Gson().toJson(allPaths));
//        LogUtil.i("head: %s", new Gson().toJson(head));
//        LogUtil.i("body: %s", new Gson().toJson(body));
//        LogUtil.i("rHand: %s", new Gson().toJson(rHand));
//        LogUtil.i("lHand: %s", new Gson().toJson(lHand));

        RichPathAnimator
                .animate(allPaths)
                .trimPathEnd(0, 1)
                .curveType(Animator.CurveType.LINEAR)
                .duration(800)
                .animationListener(new AnimationListener() {

                    @Override
                    public void onStart() {
                        head.setFillColor(Color.TRANSPARENT.getValue());
                        body.setFillColor(Color.TRANSPARENT.getValue());
                        rHand.setFillColor(Color.TRANSPARENT.getValue());
                        lHand.setFillColor(Color.TRANSPARENT.getValue());
                        rHand.setRotation(0);
                    }

                    @Override
                    public void onStop() {}
                })

//                .thenAnimate(allPaths)
//                .fillColor(Color.TRANSPARENT.getValue(), 0xFFa4c639)
//                .duration(900)

//                .thenAnimate(rHand)
//                .rotation(0, -150)
//                .curveType(Animator.CurveType.LINEAR)
//                .duration(700)

//                .thenAnimate(rHand)
//                .rotation(-130)
//                .curveType(Animator.CurveType.LINEAR)
//                .duration(500)
//                .thenAnimate(rHand)
//                .rotation(-30)
//                .curveType(Animator.CurveType.LINEAR)
//                .duration(500)

//                .thenAnimate(rHand)
//                .rotation(-130)
//                .duration(500)
//                .thenAnimate(rHand)
//                .rotation(-150)
//                .duration(500)
//                .thenAnimate(rHand)
//                .rotation(-130)
//                .duration(500)
//                .thenAnimate(rHand)
//                .rotation(-150)
//                .duration(500)

//                .thenAnimate(rHand)
//                .rotation(-150, -130, -150, -130, -150, -130, -150)
//                .duration(2000)

//                .thenAnimate(rHand)
//                .rotation(0)
//                .duration(500)
                .start();
    }

    public void openAnimationSamples(Component component) {}

    public void openCompoundViewSamples(Component component) {}

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
